<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 微信粉丝管理
 * Class Fans
 * @package app\wechat\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class Member extends BasicAdmin
{

    /**
     * 定义当前默认数据表
     * @var string
     */
    public $table = 'store_member';

    /**
     * 显示粉丝列表
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function index()
    {
        $this->title = '用户中心';
        $get = $this->request->get();
        $db = Db::name($this->table);
        $levellist = Db::name('sys_level')->select();
        $this->assign('level',$levellist);
        foreach (['phone', 'member_level'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db->order('id desc'));
    }

    /**
     * 列表数据处理
     * @param array $list
     */
    public function add()
    {
        $levellist = Db::name('sys_level')->select();
        $this->assign('level',$levellist);
        return $this->_form($this->table, 'form');
    }


    
    public function edit()
    {
        $levellist = Db::name('sys_level')->select();
        $this->assign('level',$levellist);
        return $this->_form($this->table, 'form');
    }

    /**
     * 用户禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (in_array('10000', explode(',', $this->request->post('id')))) {
            $this->error('系统超级账号禁止操作！');
        }
        if (DataService::update($this->table)) {
            $this->success("用户禁用成功！", '');
        }
        $this->error("用户禁用失败，请稍候再试！");
    }

    /**
     * 用户禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("用户启用成功！", '');
        }
        $this->error("用户启用失败，请稍候再试！");
    }

  

    public function getTreeMember($member_id =0){
        $list = [];
        $users =    Db::name('store_member a')
            ->field('a.id,a.nickname,a.create_at,a.phone,a.account_money,b.title,a.create_at')
            ->join('sys_level b',"a.member_level = b.id")
            ->where(['a.first_leader'=>$member_id])->select();
        if (!empty($users) && check_arr($users)) {
            foreach ($users as $k => &$v) {
                $v['children'] = $this->getTreeMember($v['id']);
                $list[] = $v;
            }
        }
        return $list;
    }

    //身份认证审核
    public function idaudit()
    {
        $this->title = '身份认证管理';
        $get = $this->request->get();
        $db = Db::name("store_member_idcard");
        $where = array();
        if (isset($get['type']) && $get['type'] != '') {
            $where['status'] = $get['type'];
            $db->where($where);
        }
//        sm($get);
        if (isset($get['name']) && $get['name'] != '') {
            $where['uid'] = Db::name('store_member')->where(array('phone' => $get['name']))->value('id');
            $db->where($where);
        }
        $db->order('id desc');
       return parent::_list($db);
    }
    
    //身份认证操作

    public function idpass()
    {
        $param = input('param.');
        Db::startTrans();
        if($param['status'] == 1){
            $db[] = Db::name('store_member_idcard')->where(['id'=>$param['id']])->update(['status'=>$param['status']]);
            $db[] = mlog($param['uid'],'mining',sysconf('sharenoting'),'实名认证通过赠送分享算力'.sysconf('sharenoting').'个');
            $userInfo = Db::name('store_member')->where(['id'=>$param['uid']])->find();
            if($userInfo['first_leader'] > 0){
                $content = "推广{$userInfo['nickname']},获取到分享算力:10";
                $db[] = mlog($userInfo['first_leader'], 'mining', 10, $content, 'share_dig_spread');
            }
        }elseif($param['status'] == 2){
            $db[] = Db::name('store_member_idcard')->where(['id'=>$param['id']])->update(['status'=>$param['status']]);
        }

        if (check_arr($db)){
            Db::commit();
            $this->success('操作成功');
        } else{
            Db::rollback();
            $this->error('操作失败');
        }
    }
    
    
    
    /**
     * 分销树状关系
     */
    public function member_org()
    {
        set_time_limit(0);
        $users = Db::name('store_member');
        $id = $_GET['member_id'];
        $list = Db::name('store_member')->where([
            'first_leader' => $id
        ])->select();

        foreach ($list as $key => $v) {
            $list[$key]['share'] = get_share_num($v['id']);
            //$list[$key]['team_share'] = get_team_share_num($v['id']);
//               $list[$key]['children'] = self::handleUser($v['id'], $layer);

        }

        // 伞下会员用户id
      //  $umbrella_member_ids = Db::name('store_member')->where('leaders', 'like', "%$id%")->column('id');



        return view('', [
            'id' => $id,
            'list' => $list,

        ]);
    }

    /**
     * 获取某个人下级元素
     */
    public function ajax_lower()
    {
        $id = $_GET['id'];
        $list = Db::name('store_member')->where([
            'first_leader' => $id
        ])->select();
        foreach ($list as $key => $v) {
            $list[$key]['share'] = get_share_num($v['id']);

        }

        return view('', [
            'list' => $list
        ]);
    }

}
